package com.yiqi.liebang.feature.mine.view;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.Html;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.IntroduceBo;
import com.yiqi.liebang.framework.api.UserApi;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class EditIntroduceActivity extends BaseActivity implements Toolbar.OnMenuItemClickListener, TextWatcher {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.edt_introduce)
    EditText mEdtIntroduce;
    @BindView(R.id.tv_edit_intr_count)
    TextView mTvEditIntrCount;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_edit_introduce;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initView() {
        mToolbar.inflateMenu(R.menu.menu_save);
        initToolBar(mToolbar, "编辑自我介绍", true, true);
        mToolbar.setOnMenuItemClickListener(this);
        mEdtIntroduce.addTextChangedListener(this);
    }

    @Override
    protected void initData() {
       String info=this.getIntent().getStringExtra("info");
       mEdtIntroduce.setText(TextUtils.isEmpty(info)?"":info);
        mTvEditIntrCount.setText(Html.fromHtml("<font color=\"#2AA0F8\">" +(TextUtils.isEmpty(info)?0:info.length()) + "</font>/2000"));
    }
    @Override
    public boolean onMenuItemClick(MenuItem item) {
        if (TextUtils.isEmpty(mEdtIntroduce.getText().toString())){
            showMsg("请输入自我介绍！");
            return true;
        }
        AM.api().createApiService(UserApi.class)
                .editIntroduce(new IntroduceBo(mEdtIntroduce.getText().toString()))
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(String s) {
//                        showMsg("修改成功！");
                        finish();

                    }

                    @Override
                    public void onError(Throwable e) {
                        showMsg(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
        return false;
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        mTvEditIntrCount.setText(Html.fromHtml("<font color=\"#2AA0F8\">" + s.toString().length() + "</font>/2000"));
    }
}
